<!--
* @description:新增职位
* @fileName: TheNewPosition
* @author:孟帅龙
* @date:2021-1-11
* @后台人员:孟帅龙
-->
<template>
  <div>
    <!-- 新增职位 -->
    <div class="xzzw">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="职位编号：" prop="PositionNumber">
          <el-input
            v-model="ruleForm.PositionNumber"
            placeholder="编号一般由zw+4位数组成，如zw0001"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="职位名称：" prop="PositionName">
          <el-input v-model="ruleForm.PositionName"></el-input>
        </el-form-item>
        <el-form-item label="所属部门：" prop="DeptId">
          <el-select v-model="ruleForm.DeptId" placeholder="请选择部门">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.DeptName"
              :value="item.Id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="别名：" prop="Alias">
          <el-input v-model="ruleForm.Alias"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [],
      ruleForm: {
        // PositionNumber: "",
        PositionName: "",
        Alias: "",
        DeptId: "",
      },
      rules: {
        PositionName: [
          { required: true, message: "请输入职位名称", trigger: "blur" },
        ],
        Alias: [{ required: true, message: "请输入别名", trigger: "blur" }],
        PositionNumber: [
          { required: true, message: "请输入编号", trigger: "blur" },
          { min: 6, max: 6, message: "长度必须为6个字符", trigger: "blur" },
        ],
        DeptId: [
          { required: true, message: "请选择所属部门", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    goBack() {
      this.$router.push("/PositionManagement");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          this.$axios
            .post(
              "https://localhost:44399/api/app/position/add-position",
              this.ruleForm
            )
            .then((res) => {
              if (res.data > 0) {
                this.$message.success("添加成功");
                this.$router.push("/PositionManagement");
              } else {
                this.$message.error("添加失败");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //部门显示
    loadDept() {
      this.$axios
        .get("https://localhost:44399/api/app/operator/department")
        .then((res) => {
          this.options = res.data;
        });
    },
  },
  created() {
    this.loadDept();
  },
};
</script>

<style>
.xzzw {
  width: 500px;
}
</style>